package com.ramos.kuaixiao.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.ramos.kuaixiao.entity.CustomerStore;
import com.ramos.kuaixiao.entity.SaleCustomerStore;

import java.util.ArrayList;
import java.util.List;

public class CustomerStoreDB {
    private static String TAG = "CustomerStoreDB";
    private static String TABLE_NAME = "CustomerStore";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "id" + " CHAR(30) PRIMARY KEY  NOT NULL,"
                + "qid" + " CHAR(30)," + "name" + " CHAR(30)," +"address" + " CHAR(30)," + "city" + " CHAR(30)," + "street" +
                " CHAR(30)," + "mobilePhone" + " CHAR(30),"  + "customerId" + " CHAR(30),"  + "modifyTime" + " Long);";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    public int addAllCustomer(List<CustomerStore> customer) {
        //deleteAll();
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM "+TABLE_NAME);
        String sql = "insert into "+ TABLE_NAME+" (qid,id,name,address,city,street," +
                ",mobilePhone,customerId,modifyTime) values(?,?,?,?,?,?,?,?,?)";
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < customer.size(); i++) {
            stmt.bindString(1, customer.get(i).getUid() + "");
            stmt.bindString(2, customer.get(i).getId() + "");
            stmt.bindString(3, customer.get(i).getName() + "");
            stmt.bindString(4, customer.get(i).getAddress() + "");
            stmt.bindString(5, customer.get(i).getCity() + "");
            stmt.bindString(6, customer.get(i).getStreet() + "");
            stmt.bindString(7, customer.get(i).getMobilePhone() + "");
            stmt.bindString(8, customer.get(i).getCustomerId() + "");
            stmt.bindString(9, customer.get(i).getModifyTime() + "");
            stmt.execute();
            stmt.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }


    public int updateAllCustomer(List<CustomerStore> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        Log.d(TAG, "updateAllCustomer:" + customer.size());
        for (int i = 0; i < customer.size(); i++) {
            CustomerStore firstWarehouse = customer.get(i);
            ContentValues cv = new ContentValues();
            cv.put("id", firstWarehouse.getId() + "");
            cv.put("qid", firstWarehouse.getQid() + "");
            cv.put("name", firstWarehouse.getName() + "");
            cv.put("address", firstWarehouse.getAddress() + "");
            cv.put("city", firstWarehouse.getCity() + "");
            cv.put("street", firstWarehouse.getStreet() + "");
            cv.put("mobilePhone", firstWarehouse.getMobilePhone() + "");
            cv.put("customerId", firstWarehouse.getCustomerId() + "");
            cv.put("modifyTime", firstWarehouse.getModifyTime() + "");
            db.insertWithOnConflict(TABLE_NAME, null, cv, SQLiteDatabase.CONFLICT_REPLACE); //如果有记录，则更新，并返回1，否则返回0
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public long getMaxModifyTime() {
        long param = 0;
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select max(modifyTime)from "+TABLE_NAME +" where modifyTime > 0" , new String[]{});
        Log.d(TAG, "getMaxModifyTime:getCount" + cursor.getCount());
        if (cursor.moveToFirst()) {
            param = cursor.getLong(0);
        }
        cursor.close();
        Log.d(TAG, "getMaxModifyTime:param" + param);
        return param;
    }

    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM "+TABLE_NAME);
    }

    public CustomerStore findCustomerStore(String customerId) {
        Log.d(TAG, "findCustomerStore:" + customerId);
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from "+ TABLE_NAME+" where customerId=?", new String[]{customerId});
        Log.d(TAG, "cursor:" + cursor.getCount());
        if(cursor.moveToNext()){
            CustomerStore customer = new CustomerStore();
            customer.setId(cursor.getString(0));
            customer.setQid(cursor.getString(1));
            customer.setName(cursor.getString(2));
            customer.setAddress(cursor.getString(3));
            customer.setCity(cursor.getString(4));
            customer.setStreet(cursor.getString(5));
            customer.setMobilePhone(cursor.getString(6));
            customer.setCustomerId(cursor.getString(7));
            customer.setWareHouseList(cursor.getString(8));
            cursor.close();
            return customer;
        }
        cursor.close();
        return null;
    }

    public boolean hasData() {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select count(*)from "+TABLE_NAME, null);
        cursor.moveToFirst();
        Long count = cursor.getLong(0);
        if (count > 0) {
            cursor.close();
            Log.d(TAG, "hasCustomerStore true");
            return true;
        } else {
            Log.d(TAG, "hasCustomerStore false");
            cursor.close();
            return false;
        }
    }

    public List<CustomerStore> findCustomerStore(List<SaleCustomerStore> tmpList) {
        List<CustomerStore> list = new ArrayList<>();
        for(SaleCustomerStore store : tmpList){
            Log.d(TAG ,"store.getStore_id():"+store.getStoreId());
            CustomerStore customer = findCustomerStore(store.getStoreId());
            if(customer != null) {
                list.add(customer);
            }
        }
        return list;
    }
}
